using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

using ipi.MCC.Global;
using System.Reflection;

namespace ipi.MCC.Features.EventHandler
{
	class MCCMetaData : SPFeatureReceiver
	{
		public override void FeatureInstalled(SPFeatureReceiverProperties properties)
		{
		}

		public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
		{
		}

		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			if (properties.Feature.Parent is SPSite)
			{
                try
                {

                    SPSite spsCurrent = (SPSite)properties.Feature.Parent;
                    SPWeb spwCurrent = spsCurrent.RootWeb;
                    SPList splTreeList = spwCurrent.Lists[ipi.MCC.Global.Functions.TreeList_Title];

                    SPFieldLookup sflParentNode = (SPFieldLookup)splTreeList.Fields.GetFieldByInternalName("ParentNode");
                    if (sflParentNode != null)
                    {
                        sflParentNode.LookupList = splTreeList.ID.ToString();
                        sflParentNode.LookupField = "ID";
                        sflParentNode.Update();
                    }

                    if (splTreeList.HasUniqueRoleAssignments)
                    {
                        splTreeList.ResetRoleInheritance();
                    }
                    splTreeList.BreakRoleInheritance(false);

                    SPRoleAssignmentCollection racCurrent = splTreeList.RoleAssignments;

                    SPRoleDefinition rdReader = null;
                    SPRoleDefinition rdFull = null;
                    foreach (SPRoleDefinition rdCurrent in SPContext.Current.Web.RoleDefinitions)
                    {
                        switch (rdCurrent.Type)
                        {
                            case SPRoleType.Administrator:
                                rdFull = rdCurrent;
                                break;

                            case SPRoleType.Reader:
                                rdReader = rdCurrent;
                                break;
                        }
                    }

                    foreach (SPUser spuAdmin in spwCurrent.SiteAdministrators)
                    {
                        SPRoleAssignment raAdmin = new SPRoleAssignment(spuAdmin);
                        raAdmin.RoleDefinitionBindings.Add(rdFull);
                        racCurrent.Add(raAdmin);
                    }
                    
                    SPRoleAssignment raReadMember = new SPRoleAssignment(spwCurrent.AssociatedMemberGroup);
                    raReadMember.RoleDefinitionBindings.Add(rdReader);
                    racCurrent.Add(raReadMember);

                    SPRoleAssignment raReadOwner = new SPRoleAssignment(spwCurrent.AssociatedOwnerGroup);
                    raReadOwner.RoleDefinitionBindings.Add(rdReader);
                    racCurrent.Add(raReadOwner);

                    SPRoleAssignment raReadVisitor = new SPRoleAssignment(spwCurrent.AssociatedVisitorGroup);
                    raReadVisitor.RoleDefinitionBindings.Add(rdReader);
                    racCurrent.Add(raReadVisitor);

                    //spsCurrent.SystemAccount
                    //spsCurrent.Owner
                    //spsCurrent.RootWeb.SiteAdministrators

                    /*                SPFieldLookup sflTreePosition = (SPFieldLookup)spwCurrent.Fields.GetField("TreePosition");
                                    if (sflTreePosition != null)
                                    {
                                        sflTreePosition.LookupList = splTreeList.ID.ToString();
                                        sflTreePosition.LookupField = "ID";
                                        sflTreePosition.AllowMultipleValues = true;
                                        sflTreePosition.Update();
                                    }*/
                }
                catch (Exception exc)
                {
                    Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Global_Exception);
                }
			}
		}

		// uninstall custom timer job
		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
		}
	}
}
